<template>
	<view class="add">
		<navBar title="添加技师"></navBar>

		<view class="title">上传图片</view>

		<view class="images flex_center">
			<view class="image_box" v-for="(item,index) in images" :key="index">
				<image :src="item" mode="aspectFill"></image>
				<view class="icon_detele" @click="onDeteleImage(index)">
					<u-icon name="close-circle-fill" size="36rpx" color="#FD2A53"></u-icon>
				</view>
			</view>
			<view class="images_box flex_r_around" v-if="images.length < 3" @click="onImages">
				<u-icon name="plus" color="#666666" size="40rpx" bold></u-icon>
			</view>
		</view>

		<view class="name flex_r_between">
			<input type="text" v-model="info.username" placeholder="请输入技师姓名" placeholder-class="input-placeholder" />
			<picker @change="onSex" range-key="name" :range="sexList">
				<view class="picker flex_r_between">
					<text :id="info.sex ? 'have' : ''">{{info.sex == 1 ? '男' : info.sex == 2 ? '女' : '请选择技师性别'}}</text>
					<u-icon name="arrow-right" color="#131313" size="24rpx" bold></u-icon>
				</view>
			</picker>
		</view>

		<view class="input">
			<input type="number" v-model="info.seniority" placeholder="请输入技师工龄" placeholder-class="input-placeholder" />
		</view>

		<view class="input" @click="chooseLocation">
			<view class="flex_center" :id="info.province ? 'have' : ''">
				<text
					class="hidden">{{info.province ? `${info.province}-${info.city}-${info.district}` : '请选择技师位置'}}</text>
				<u-icon name="arrow-right" color="#131313" size="24rpx" bold></u-icon>
			</view>
		</view>

		<view class="title">介绍</view>

		<view class="text_content">
			<textarea maxlength="200" placeholder-class="input-placeholder" v-model="info.content"
				placeholder="请输入技师介绍">
			 </textarea>

			<view class="tag">
				{{info.content.length}}/200
			</view>
		</view>

		<view class="btn" @click="onsubmit">确认提交</view>

		<u-modal :show="showmodal" title="提示" content='添加成功' confirmText="我知道了" @confirm="back"></u-modal>

		<w-compress ref='wCompress' />

	</view>
</template>

<script>
	import navBar from '@/compentents/navBar/navBar.vue';
	import WCompress from '@/components/w-compress/w-compress.vue';
	export default {
		components: {
			navBar,
			WCompress,
		},
		data() {
			return {
				images: [],
				info: {
					// 展示图
					images: [],
					// 技师姓名
					username: '',
					// 性别：1=男，2=女
					sex: 0,
					// 工龄
					seniority: '',
					// 省市区 经纬度
					province: '',
					city: '',
					district: '',
					lng: '',
					lat: '',
					// 技师介绍
					content: '',
					id: 0,
				},


				sexList: [{
						name: '男',
						sex: 1,
					},

					{
						name: '女',
						sex: 2,
					},
				],

				showmodal: false,
			}
		},
		methods: {
			onDeteleImage(index) {
				this.images.splice(index, 1);
			},
			onImages() {
				this.$native.authorize('photos');
				var count = 1;

				uni.chooseImage({
					count: count,
					crop: {
						width: 600,
						ieght: 600,
					},
					success: (file) => {
						file.tempFilePaths.forEach(element => {
							uni.navigateTo({
								url: `/pages/index/cropper?src=${element}`,
								events: {
									getImage: (data) => {
										this.images.push(data.src)
									}
								}
							});
							// this.$refs.wCompress.start(element, {
							// 	pixels: 2000000, // 最大分辨率，默认二百万
							// 	quality: 0.6, // 压缩质量，默认0.8
							// 	type: 'png', // 图片类型，默认jpg
							// 	// base64: false, // 是否返回base64，默认false，非H5有效
							// }).then(url => {
							// 	this.$upload(url, {
							// 		type: 'picture'
							// 	}).then(res => {
							// 		if (res.status == 2 || res.status == 1) {
							// 			this.images.push(res.result.url)
							// 		} else {
							// 			this.$util.msg(res.msg)
							// 		}
							// 	})
							// });
						})
					}
				})
			},

			onSex(e) {
				this.info.sex = this.sexList[e.detail.value].sex;
			},

			chooseLocation() {
				uni.chooseLocation({
					success: (loca) => {
						this.info.lat = loca.latitude;
						this.info.lng = loca.longitude;
						this.$get('index/getAddressByLocation', {
							location: `${loca.latitude},${loca.longitude}`
						}).then(res => {
							if (res.data.status == 0) {
								var result = res.data.result;
								this.info.province = result.addressComponent.province;
								this.info.city = result.addressComponent.city;
								this.info.district = result.addressComponent.district;
							} else {
								this.$util.msg('地址解析失败')
							}
						})
					}
				})
			},

			onsubmit() {
				this.info.images = this.images.join(',');
				this.$post('/store.technician/add', this.info).then(res => {
					this.showmodal = true;
				});
			},

			back() {
				this.showmodal = false;
				this.$util.back();
			},
		},

		onLoad(option) {
			if(option.data){
				var data = JSON.parse(option.data);
				if (data.images) {
					if (data.images.indexOf(',') > -1) {
						data.images = data.images.split(',');
					} else {
						data.images = [data.images];
					}
				}
				this.images = data.images;
				this.info = data;
			}
		}
	}
</script>

<style lang="less" scoped>
	.add {
		background: #FFFFFF;
		min-height: 100vh;
		padding: 32rpx 32rpx 40rpx;
	}

	.title {
		font-weight: 600;
		font-size: 32rpx;
		color: #131313;
		margin-bottom: 32rpx;
	}

	.images {

		.image_box {
			width: 216rpx;
			height: 216rpx;
			position: relative;
			margin: 0 6rpx;

			.icon_detele {
				position: absolute;
				right: 0;
				top: 0;
				padding: 10rpx;
			}

			image {
				width: 216rpx;
				height: 216rpx;
				background: #F8F8F8;
				border-radius: 24rpx;
			}
		}

		.images_box {
			width: 216rpx;
			height: 216rpx;
			background: #F8F8F8;
			border-radius: 24rpx;
			margin: 0 6rpx;
		}
	}

	.name {
		margin: 30rpx 0 16rpx;

		input {
			width: 336rpx;
			height: 100rpx;
			background: #F8F8F8;
			border-radius: 16rpx;
			padding: 0 32rpx;
			font-size: 28rpx;
			color: #333;
			box-sizing: border-box;
		}

		.picker {
			width: 336rpx;
			height: 100rpx;
			background: #F8F8F8;
			border-radius: 16rpx;
			padding: 0 32rpx;

			text {
				font-size: 28rpx;
				color: #999;
			}
		}
	}

	.input {
		padding: 0 32rpx;
		background: #F8F8F8;
		border-radius: 16rpx;
		margin-bottom: 16rpx;

		input {
			height: 100rpx;
			font-size: 28rpx;
			color: #333;
		}

		view {
			height: 100rpx;
			font-size: 28rpx;
			color: #999999;
			line-height: 100rpx;

			text {
				flex: 1;
			}


		}
	}

	.text_content {
		background: #F8F8F8;
		border-radius: 16rpx;
		padding: 24rpx;

		textarea {
			font-size: 28rpx;
			color: #333;
			line-height: 44rpx;
		}

		.tag {
			text-align: right;
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}
	}

	.btn {
		height: 100rpx;
		background: #FD2A53;
		border-radius: 50rpx;
		font-weight: 600;
		font-size: 32rpx;
		color: #FFFFFF;
		line-height: 100rpx;
		text-align: center;
		margin-top: 120rpx;
	}

	#have {
		color: #333;
	}
</style>